<template>
    <el-upload
        class="upload-demo"
        :action="action"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        :on-success = "uploadSuccess"
        multiple
        :limit="1"
        :on-exceed="handleExceed"
        :file-list="fileList">
        <el-button size="small" icon="el-icon-upload" type="primary">点击上传</el-button>

        <a slot="tip" v-show="mubanShow"  href="/static/txt测试文件.txt"  download="测试文件.txt" style="color:#3D7EFF">
            下载模板
        </a>
        <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
    </el-upload>

</template>

<script>

export default {
    components: { },
    props: {
        action:{
            type: String,
            default: '/bhtong/common/file/upload'
        },
        filename:{
            type:String,
            default:''
        },
        mubanShow:{
            type:Boolean,
            default:false
        }
    },
    data(){
        return{
            fileList:[]
        }
    },
    computed: {
  
    },
    watch:{
        filename:function(n,o){
            var new_name = n.slice(33)
            if(new_name){
                this.fileList.push({ name:new_name, url: n })
            }
        } 
    },
    methods: {

        //点击打开文件
        handlePreview(file) {
            var val ='';
            if(file.url){
                val = file.url
            }else{
                val = file.response.tdata
            }
            var search = toSearch({
                fileName:val
            });
            window.open('/bhtong/common/file/download' + search)
        },
        //上传成功时
        uploadSuccess:function(response, file,fileList){
            this.$emit('setfile',response.tdata);
        },

        handleExceed(files, fileList) {
            this.$message.warning(`当前限制选择 1 个文件，请移除当前文件后，在进行上传`);
        },
        //文件移除之前
        beforeRemove(file, fileList) {
            return this.$confirm(`确定移除 ${ file.name }？`);
        },
        //文件移除时
        handleRemove(file, fileList) {
            this.$emit('setfile','');
            this.fileList = []
        },
    },
    mounted:function(){

    }
}
</script>

<style scoped >



</style>
